﻿using com.iot.core.annotation; 
using com.iot.core.data; 
using com.iot.core.infrastructure;  
using com.iot.ht.Controllers.htsoft.service;
using com.iot.ht.Controllers.htuser.data; 
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using com.iot.core.ext;
using Icom.iot.core.ext;
using com.iot.ht.Controllers.htsoft.data;
using com.iot.core.manager.id;
using Microsoft.EntityFrameworkCore.Query;
using com.iot.ht.Controllers.httask.data;
using com.iot.ht.Controllers.httask.service;

namespace com.iot.ht.Controllers.httask
{
    [ApiController]
    [Route("[controller]/[action]")]
    [Tag(Name = "后台任务", Description = "")]
    public class HtTaskController : ControllerBase
    {
        readonly IRepository<HtTaskLog> htTaskLogMapper;
        public HtTaskController(IRepository<HtTaskLog> p1)
        {
            htTaskLogMapper = p1;   
        }


        [HttpPost]
        [Operation(Summary = "任务分页", Description = "")]
        [Auth(AuthType.Login)]
        [Log(LogNeed = false, LogPin = false, LogPout = false, LogTemplete = "")]
        public async Task<Page<HtTaskAppStateDto>> PageAsync([FromBody] BasePageQto mm)
        {
            var l = Singleton<HtTaskService>.Instance.Apps;
            if (!string.IsNullOrWhiteSpace(mm.Key))
            {
                l = l.Where(a => a.Name.Contains(mm.Key)).ToList();
            }
            var r = new Page<HtTaskAppStateDto>(l.Count, mm.Page, mm.PageSize);
            r.Records = l.AsQueryable().SortPageTake(new Sort(), r);
            return r;
        }


        [HttpPost]
        [Operation(Summary = "任务日志分页", Description = "")]
        [Auth(AuthType.Login)]
        [Log(LogNeed = false, LogPin = false, LogPout = false, LogTemplete = "")]
        public async Task<Page<HtTaskLog>> PageLogAsync([FromBody] BasePageQto mm)
        {
            var q = htTaskLogMapper.GetAll();
            if (mm.Id > 0)
            {
                q = q.Where(a => a.HttaskappId == mm.Id);
            }
            if (!string.IsNullOrEmpty(mm.Key))
            {
                q = q.Where(a => a.Msg.Contains(mm.Key));
            }
            var r = await htTaskLogMapper.PageAsync(q, mm);
            return r;
        }


        [HttpPost]
        [Operation(Summary = "任务详情", Description = "")]
        [Auth(AuthType.Login)]
        [Log(LogNeed = false, LogPin = false, LogPout = false, LogTemplete = "")]
        public async Task<HtTaskApp> DetailAsync(BaseQto qm)
        {
            var l = Singleton<HtTaskService>.Instance.Apps;
            var m = l.Where(a => a.Id == qm.Id).FirstOrDefault();
            return m.HtTaskApp;
        }

        [HttpPost]
        [Operation(Summary = "插入初始化", Description = "")]
        [Auth(AuthType.Login)]
        [Log(LogNeed = false, LogPin = false, LogPout = false, LogTemplete = "")]
        public HtTaskApp InsertInit()
        {
            var Sortno = Singleton<HtTaskService>.Instance.HtTaskApps.Max(a => (long?)a.Sortno);

            var mm = new HtTaskApp
            {
                Id = HtIdManager.Current.NextLong(),
               
                Sortno = 10,
                AutoStart = true,
                Interval = 10000,
                 
            };
            if (Sortno.HasValue)
            {
                mm.Sortno = Sortno.Value + 10;
            }
            return mm;
        }


        [HttpPost]
        [Operation(Summary = "添加任务", Description = "")]
        [Auth(AuthType.Auth)]
        [Log(LogNeed = true, LogPin = true, LogPout = true, LogTemplete = "")]
        public int Insert(HtTaskApp mm)
        {
            var r = Singleton<HtTaskService>.Instance.Insert(mm);
            return r;
        }

        [HttpPost]
        [Operation(Summary = "修改任务", Description = "")]
        [Auth(AuthType.Auth)]
        [Log(LogNeed = true, LogPin = true, LogPout = true, LogTemplete = "")]
        public int Update(HtTaskApp mm)
        {
            var r = Singleton<HtTaskService>.Instance.Update(mm);
            return r;
        }


        [HttpPost]
        [Operation(Summary = "删除任务", Description = "")]
        [Auth(AuthType.Auth)]
        [Log(LogNeed = true, LogPin = true, LogPout = true, LogTemplete = "")]
        public int Delete(BaseQto mm)
        {
            var r = Singleton<HtTaskService>.Instance.Delete(mm);
            return r;
        }



        [HttpPost]
        [Operation(Summary = "启动任务", Description = "")]
        [Auth(AuthType.Auth)]
        [Log(LogNeed = true, LogPin = true, LogPout = true, LogTemplete = "")]
        public async Task<int> Start(BaseQto mm)
        {
            var r = await Singleton<HtTaskService>.Instance.StartSingleApp(mm);
            return r;
        }

        [HttpPost]
        [Operation(Summary = "停止任务", Description = "")]
        [Auth(AuthType.Auth)]
        [Log(LogNeed = true, LogPin = true, LogPout = true, LogTemplete = "")]
        public int Stop(BaseQto mm)
        {
            var r = Singleton<HtTaskService>.Instance.StopSingleApp(mm);
            return r;
        }

    }
}
